package club.hicode.daydayup.sslFile.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class SecureChatClient {

	private final String host;
	private final int port;
	private final String sslMode;

	public SecureChatClient(String host, int port, String sslMode) {
		this.host = host;
		this.port = port;
		this.sslMode = sslMode;
	}

	public void run() throws Exception {
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(NioSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
			.handler(new SecureChatClientInitializer(sslMode));
			// Start the connection attempt.
			Channel ch = b.connect(host, port).sync().channel();
			//等待客户端链路关闭
			ch.closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			// The connection is closed automatically on shutdown.
			group.shutdownGracefully();
		}
	}

	public static void main(String[] args) throws Exception {
		// Parse options.
		String sslMode = "CA";
		new SecureChatClient("127.0.0.1", 8443, sslMode).run();
	}
}
